/*
 * @Author: your name
 * @Date: 2021-04-03 19:57:29
 * @LastEditTime: 2021-04-19 11:24:59
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \chronic-back-end\module\userMod\model.go
 */
package userMod

import (
	"github.com/jinzhu/gorm"
	"golang.org/x/crypto/bcrypt"
)

type Role uint

const (
	Patient = Role(iota)
	Doctor
	Admin
	Root
)

type User struct {
	gorm.Model
	Name           string
	Role           Role   `gorm:"not null;default:0"`
	Phone          string `gorm:"unique_index;not null"`
	Password       string `gorm:"not null"`
	Gender         string
	Birthday       string
	Disease        string // 患者选填
	Deal           uint   // 签约医生ID 患者选填
	DoctorDegree   string // 医生选填
	DoctorHospital string // 医生选填
	DoctorLevel    string // 医生选填
}

// Database will only save the hashed string, you should check it by util function.
// 	if err := serModel.CheckPassword("password0"); err != nil { password error }
func (u *User) CheckPassword(password string) error {
	bytePassword := []byte(password)
	byteHashedPassword := []byte(u.Password)
	return bcrypt.CompareHashAndPassword(byteHashedPassword, bytePassword)
}
